import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import './styles.scss'
import localStorage from 'localStorage'
Vue.prototype.$localStorage = localStorage
Vue.use(ElementUI)

Vue.config.productionTip = false
const tree = [{ id: '10000', name: 'root', type: 0, desc: '根节点' }]
const types = [
  { name: "Object", label: "Json", value: "0" },
  { name: "String", label: "字符串", value: "1" },
  { name: "Number", label: "数字", value: "2" },
  { name: "Date", label: "时间", value: "3" },
  { name: "Boolean", label: "布尔", value: "4" },
  { name: "Array", label: "数组", value: "5" }
]
Vue.prototype.$types = types
//过滤器
Vue.filter('type', function (value) {
  for (let index = 0; index < types.length; index++) {
    const element = types[index];
    if (element.value == value) return element.label
  }
  return ""
})
Vue.prototype.getLabel = function (value) {
  for (let index = 0; index < types.length; index++) {
    const element = types[index];
    if (element.value == value) return element.label
  }
  return ""
}
//本地存储（初始化生成）
// localStorage.setItem('tree', JSON.stringify(tree))
localStorage.setItem('types', JSON.stringify(types))
if (localStorage.getItem('trees')) {
  Vue.prototype.$trees = JSON.parse(localStorage.getItem('trees'))
} else {
  Vue.prototype.$trees = []
}
//创建id
Vue.prototype.$GUID = function () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0,
      v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}
//获取所有模板，或者id获取单个模板
Vue.prototype.$getMB = function (id) {
  let MB = []
  if (localStorage.getItem('MB')) {
    MB = JSON.parse(localStorage.getItem('MB'))
  }
  if (!id) return MB
  for (let index = 0; index < MB.length; index++) {
    const element = MB[index];
    if (element.id == id) return element
  }
  return null
}
Vue.prototype.$delMB = function (id) {
  let MB = JSON.parse(localStorage.getItem('MB'))
  for (let index = 0; index < MB.length; index++) {
    const element = MB[index];
    if (element.id == id) {
      MB.splice(index, 1)
      break
    }
  }
  localStorage.setItem('MB', JSON.stringify(MB))
}
//添加到模板
Vue.prototype.$setMB = function (item) {
  let MB = []
  if (localStorage.getItem('MB')) {
    MB = JSON.parse(localStorage.getItem('MB'))
  }
  MB.push(item)
  localStorage.setItem('MB', JSON.stringify(MB))
}
//获取所有模板列表，或者id获取单个模板组
Vue.prototype.$getMBList = function (temID) {
  let MBList = []
  if (localStorage.getItem('MBList')) {
    MBList = JSON.parse(localStorage.getItem('MBList'))
  }
  if (!temID) return MBList
  let arr = []
  for (let index = 0; index < MBList.length; index++) {
    const element = MBList[index];
    if (element.temID == temID) arr.push(element)
  }
  console.log(arr)
  return arr
}
Vue.prototype.$delMBList = function (id) {
  let MBList = JSON.parse(localStorage.getItem('MBList'))
  for (let index = 0; index < MBList.length; index++) {
    const element = MBList[index];
    console.log(element.id == id)
    if (element.id == id) {
      MBList.splice(index, 1)
      break
    }
  }
  localStorage.setItem('MBList', JSON.stringify(MBList))
}
//添加到模板
Vue.prototype.$setMBList = function (item) {
  let MBList = []
  if (localStorage.getItem('MBList')) {
    MBList = JSON.parse(localStorage.getItem('MBList'))
  }
  MBList.push(item)
  localStorage.setItem('MBList', JSON.stringify(MBList))
}
//获取关联表所有数据，或者id获取单个模板
Vue.prototype.$getGL = function (id) {
  let GL = []
  if (localStorage.getItem('GL')) {
    GL = JSON.parse(localStorage.getItem('GL'))
  }
  if (!id) return GL
  for (let index = 0; index < GL.length; index++) {
    const element = GL[index];
    if (element.id == id) return element
  }
  return null
}
Vue.prototype.$delGL = function (id) {
  let GL = JSON.parse(localStorage.getItem('GL'))
  for (let index = 0; index < GL.length; index++) {
    const element = GL[index];
    if (element.id == id) {
      GL.splice(index, 1)
      break
    }
  }
  localStorage.setItem('GL', JSON.stringify(GL))
}
//添加到关联表
Vue.prototype.$setGL = function (item) {
  let GL = []
  if (localStorage.getItem('GL')) {
    GL = JSON.parse(localStorage.getItem('GL'))
  }
  GL.push(item)
  localStorage.setItem('GL', JSON.stringify(GL))
}
//获取所有模板列表，或者id获取单个模板组
Vue.prototype.$getGLList = function (glID) {
  let GLList = []
  if (localStorage.getItem('GLList')) {
    GLList = JSON.parse(localStorage.getItem('GLList'))
  }
  if (!glID) return GLList
  let arr = []
  for (let index = 0; index < GLList.length; index++) {
    const element = GLList[index];
    if (element.GLid == glID) arr.push(element)
  }
  return arr
}
//获取所有模板列表，或者id获取单个模板组
Vue.prototype.$delGLList = function (id) {
  let GLList = JSON.parse(localStorage.getItem('GLList'))
  for (let index = 0; index < GLList.length; index++) {
    const element = GLList[index];
    if (element.id == id){
      GLList.splice(index, 1)
      break
    }  
  }
  localStorage.setItem('GLList', JSON.stringify(GLList))
}
//添加到模板
Vue.prototype.$setGLList = function (item) {
  let GLList = []
  if (localStorage.getItem('GLList')) {
    GLList = JSON.parse(localStorage.getItem('GLList'))
  }
  GLList.push(item)
  localStorage.setItem('GLList', JSON.stringify(GLList))
}
//
function _treeFn(data, pid) {
  const result = []
  let temp = []
  for (let i = 0; i < data.length; i++) {
    if (data[i].parentID === pid) {
      const obj = { 'label': data[i].name, 'id': data[i].id, ...data[i] }
      temp = _treeFn(data, data[i].id)
      if (temp.length > 0) {
        obj.children = temp
      }
      result.push(obj)
    }
  }
  return result
}
Vue.prototype.$treeFn = _treeFn
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
